package cn.qayy.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import cn.qayy.constants.CollectionConstant;
import cn.qayy.constants.Constant;
import cn.qayy.service.Uploadable;
import cn.qayy.utils.DateUtils;
import cn.qayy.utils.ServletContextUtils;
import cn.qayy.utils.StringUtil;

/***
 *@author wy
 *流向表，批量导入的DTO
 */
/**
 * @author jobs1127
 *
 */
@SuppressWarnings("all")
public class PlansDTO extends commonDTO implements Serializable,Uploadable{
	/**
	 * 销售行为															
	 */
    private String judge ="";
    /**
     * 销售日期
     */
    private String sdate ="";
    /**
     * 出货甲方
     */
    private String commercial1 ="";
    /**
     * 入货乙方
     */
    private String terminal ="";
    /**
     * 品种
     */
    private String product ="";
    /**
     * 包装
     */
    private String pack ="";
    /**
     * 数量
     */
    private String count ="";
    /**
     * 批号
     */
    private String pihao ="";
    /**
     * 备注
     */
    private String demo ="";
    /**
     * 客户
     */
    private String client ="";
    /**
     * 模式
     */
    private String mode	="";
    /**
     * 晚交流向标识
     */
    private String money6 ="";
    /**
     * 真自然流向标识
     */
    private String status6 ="";
    /**
     * 不合规流向
     */
    private String unqualified ="";
    /**
     * 条线
     */
    private String xieyizhuangtai ="";
    /**
     * 修订标识
     */
    private String isUpdate ="E";
    /**
     * ID列(请勿动)
     */
    private String id ="";

    public String getIsUpdate() {
        return this.isUpdate;
    }

    public void setIsUpdate(String isUpdate) {
        this.isUpdate = isUpdate;
    }

    public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getXieyizhuangtai() {
        return this.xieyizhuangtai;
    }

    public void setXieyizhuangtai(String xieyizhuangtai) {
        this.xieyizhuangtai = xieyizhuangtai;
    }

    public String getJudge() {
        return this.judge;
    }

    public void setJudge(String judge) {
        this.judge = judge;
    }

    public String getSdate() {
        return this.sdate;
    }

    public void setSdate(String sdate) {
        this.sdate = sdate;
    }

    public String getCommercial1() {
        return this.commercial1;
    }

    public void setCommercial1(String commercial1) {
        this.commercial1 = commercial1;
    }

    public String getTerminal() {
        return this.terminal;
    }

    public void setTerminal(String terminal) {
        this.terminal = terminal;
    }

    public String getProduct() {
        return this.product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

    public String getPack() {
        return this.pack;
    }

    public void setPack(String pack) {
        this.pack = pack;
    }

    public String getCount() {
        return this.count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public String getPihao() {
        return this.pihao;
    }

    public void setPihao(String pihao) {
        this.pihao = pihao;
    }

    public String getDemo() {
        return this.demo;
    }

    public void setDemo(String demo) {
        this.demo = demo;
    }

    public String getClient() {
        return this.client;
    }

    public void setClient(String client) {
        this.client = client;
    }

    public String getMode() {
        return this.mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getMoney6() {
        return this.money6;
    }

    public void setMoney6(String money6) {
        this.money6 = money6;
    }

    public String getStatus6() {
        return this.status6;
    }

    public void setStatus6(String status6) {
        this.status6 = status6;
    }
    
	@Override
	public String toString() {
		return "PlansDTO [judge=" + judge + ", sdate=" + sdate + ", commercial1=" + commercial1 + ", terminal="
				+ terminal + ", product=" + product + ", pack=" + pack + ", count=" + count + ", pihao=" + pihao
				+ ", demo=" + demo + ", client=" + client + ", mode=" + mode + ", money6=" + money6 + ", status6="
				+ status6 + ", unqualified=" + unqualified + ", xieyizhuangtai=" + xieyizhuangtai + ", isUpdate="
				+ isUpdate + ", id=" + id + "]";
	}

	public PlansDTO() {
		super();
	}

	public PlansDTO(String judge, String sdate, String commercial1, String terminal, String product, String pack,
			String count, String pihao, String demo, String client, String mode, String money6, String status6,String unqualified,
			String xieyizhuangtai, String isUpdate, String id) {
		super();
		this.judge = judge;
		this.sdate = sdate;
		this.commercial1 = commercial1;
		this.terminal = terminal;
		this.product = product;
		this.pack = pack;
		this.count = count;
		this.pihao = pihao;
		this.demo = demo;
		this.client = client;
		this.mode = mode;
		this.money6 = money6;
		this.status6 = status6;
		this.unqualified = unqualified;
		this.xieyizhuangtai = xieyizhuangtai;
		this.isUpdate = isUpdate;
		this.id = id;
	}

	@Override
	public Uploadable filter(Map<String, List<Uploadable>> map, Set<Uploadable> set, int num) {
		/**容纳错误消息的list*/
		List<Uploadable> mlist = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		/**容纳正确的，最终用于保存到数据库的数据**/
		List<Uploadable> list = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
	    
		/***
		 * 过滤掉所有的空格
		 */
		String judge = StringUtil.trimAll(this.getJudge());
	    String sdate = StringUtil.trimAll(this.getSdate());
	    String commercial1 = StringUtil.trimAll(this.getCommercial1());
	    String terminal =StringUtil.trimAll(this.getTerminal());
	    String product =StringUtil.trimAll(this.getProduct());
	    String pack =StringUtil.trimAll(this.getPack());
	    String count =StringUtil.trimAll(this.getCount());
	    String pihao =StringUtil.trimAll(this.getPihao());
	    String demo =StringUtil.trimAll(this.getDemo());
	    String client =StringUtil.trimAll(this.getClient());
	    String mode	=StringUtil.trimAll(this.getMode());
	    String money6 =StringUtil.trimAll(this.getMoney6());
	    String status6 =StringUtil.trimAll(this.getStatus6());
	    String unqualified =StringUtil.trimAll(this.getUnqualified());
	    String xieyizhuangtai =StringUtil.trimAll(this.getXieyizhuangtai());
	    String isUpdate =StringUtil.trimAll(this.getIsUpdate());
	    String id =StringUtil.trimAll(this.getId());
	    
	    /**
	     * 判断是否导入的模板是正确的模板
	     */
	    if(num == 1) {
	    	if(!CollectionConstant.plans_mb.get(0).contains(judge)
	    			||!CollectionConstant.plans_mb.get(1).contains(sdate)
	    			||!CollectionConstant.plans_mb.get(2).contains(commercial1)
	    			||!CollectionConstant.plans_mb.get(3).contains(terminal)
	    			||!CollectionConstant.plans_mb.get(4).contains(product)
	    			||!CollectionConstant.plans_mb.get(5).contains(pack)
	    			||!CollectionConstant.plans_mb.get(6).contains(count)
	    			||!CollectionConstant.plans_mb.get(7).contains(pihao)
	    			||!CollectionConstant.plans_mb.get(8).contains(demo)
	    			||!CollectionConstant.plans_mb.get(9).contains(client)
	    			||!CollectionConstant.plans_mb.get(10).contains(mode)
	    			||!CollectionConstant.plans_mb.get(11).contains(money6)
	    			||!CollectionConstant.plans_mb.get(12).contains(status6)
	    			||!CollectionConstant.plans_mb.get(13).contains(unqualified)
	    			||!CollectionConstant.plans_mb.get(14).contains(xieyizhuangtai)
	    			||!CollectionConstant.plans_mb.get(15).contains(isUpdate)
	    			||!CollectionConstant.plans_mb.get(16).contains(id)) {
	    		MsgInfo msg = new MsgInfo(Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_NUM+num);
				mlist.add(msg);
				map.put("stop", mlist);
		    	return null;
		    }
	    } else {
	    	/***
			 * 获取项目上下文的共享数据
			 */
			ServletContext servletContext = ServletContextUtils.getServletContext();
			HashSet<String> cNames = ((HashSet<String>)servletContext.getAttribute("cNames"));
	    	/***
		     * 数据合格性校验
		     */
			if(!CollectionConstant.judges.contains(judge)) {
				MsgInfo msg = new MsgInfo(judge,"销售行为",CollectionConstant.judges.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			String date = DateUtils.parseDate(sdate);
			if(date == null) {
				MsgInfo msg = new MsgInfo(sdate,"销售日期",Constant.DATE_FORMAT,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if(!CollectionConstant.products.contains(product)) {
				MsgInfo msg = new MsgInfo(product,"品种",CollectionConstant.products.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if(!CollectionConstant.packs.contains(pack)) {
				MsgInfo msg = new MsgInfo(pack,"包装",CollectionConstant.packs.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			Float parseFloat = StringUtil.parseFloat(count);
			if(parseFloat == null) {
				MsgInfo msg = new MsgInfo(count,"数量",Constant.INTEGER,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			//销售流向必须录入客户，纯销可以不录入，通过系统自动匹配
			if(judge.contains("销售")) {
				if(StringUtil.isBlank(client)) {
					MsgInfo msg = new MsgInfo(client,"客户",Constant.NOT_BLANK_MSG,Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
			if(!CollectionConstant.modes.contains(mode)) {
				MsgInfo msg = new MsgInfo(mode,"模式",CollectionConstant.modes.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if(StringUtil.isNotBlank(money6)) {
				if(!CollectionConstant.wjlx.contains(money6)) {
					MsgInfo msg = new MsgInfo(money6,"晚交流向标识",CollectionConstant.wjlx.toString(),Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
			if(StringUtil.isNotBlank(status6)) {
				if(!CollectionConstant.zrlx.contains(status6)) {
					MsgInfo msg = new MsgInfo(status6,"真自然流向标识",CollectionConstant.zrlx.toString(),Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
			if(StringUtil.isNotBlank(unqualified)) {
				if(!CollectionConstant.unqualified.contains(unqualified)) {
					MsgInfo msg = new MsgInfo(unqualified,"不合规流向标识",CollectionConstant.unqualified.toString(),Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
			
			if(!CollectionConstant.txs.contains(xieyizhuangtai)) {
				MsgInfo msg = new MsgInfo(xieyizhuangtai,"条线",CollectionConstant.txs.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if(!CollectionConstant.cudFlags.contains(isUpdate)) {
				MsgInfo msg = new MsgInfo(isUpdate,"修订标识",CollectionConstant.cudFlags.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if("E".equals(isUpdate)) {
				if(StringUtil.isBlank(id)) {
					MsgInfo msg = new MsgInfo(isUpdate,"修订标识有误或id有误","修订标识为E或空白时，id不能为空。id为空白时，修订标识只能是N",Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
			/***
			 * 判断是否在注册库中注册
			 */
			if(!cNames.contains(commercial1)) {
				MsgInfo msg = new MsgInfo(commercial1,"出货甲方",Constant.NO_REGISTER,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if(!cNames.contains(terminal)) {
				MsgInfo msg = new MsgInfo(terminal,"入货乙方",Constant.NO_REGISTER,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			
			/***
			 * 返回处理后的结果数据
			 */
			if(!mlist.isEmpty()) {
				map.put("fail"+num, mlist);
			} 
			/***把验证通过的合格数据返回**/
			this.setAll(judge,sdate,commercial1,terminal,product,pack,count,pihao,demo,client,mode,
		    		money6,status6,unqualified,xieyizhuangtai,isUpdate,id);
			//不是空行才添加
			if(StringUtil.isNotBlank(product)&&StringUtil.isNotBlank(pack)&&StringUtil.isNotBlank(judge)) {
				set.add(this);
			}
			return this;
	    }
	    return null;
	}
	
	/**
	 * 设置所有属性
	 * @param judge
	 * @param sdate
	 * @param commercial1
	 * @param terminal
	 * @param product
	 * @param pack
	 * @param count
	 * @param pihao
	 * @param demo
	 * @param client
	 * @param mode
	 * @param money6
	 * @param status6
	 * @param xieyizhuangtai
	 * @param isUpdate
	 * @param id
	 */
	private void setAll(String judge, String sdate, String commercial1, 
			String terminal, String product,
			String pack, String count, String pihao, String demo, 
			String client, String mode, String money6,
			String status6,String unqualified,String xieyizhuangtai, String isUpdate, String id) {
		this.judge = judge;
		this.sdate = sdate;
		this.commercial1 = commercial1;
		this.terminal = terminal;
		this.product = product;
		this.pack = pack;
		this.count = count;
		this.pihao = pihao;
		this.demo = demo;
		this.client = client;
		this.mode = mode;
		this.money6 = money6;
		this.status6 = status6;
		this.unqualified = unqualified;
		this.xieyizhuangtai = xieyizhuangtai;
		this.isUpdate = isUpdate;
		this.id = id;
	}

	@Override
	public String flag() {
		return this.isUpdate;
	}

	public String getUnqualified() {
		return unqualified;
	}

	public void setUnqualified(String unqualified) {
		this.unqualified = unqualified;
	}
}
